#!/bin/sh

# default prefix
PREFIX=${PREFIX:-/usr/local}

usage()
{
    echo "
Usage: $0 [--prefix=PREFIX] [--uninstall]

By default, flac2any is installed to $PREFIX.  You may change this by either
specifying where to install to via the --prefix=PREFIX option or by changing
the PREFIX environment variable.

If the --uninstall option is given, this will remove any files installed.
"
    exit 1
}

uninstall()
{
    if [ -f manifest ]; then
        for f in $(cat manifest); do
            rm -f "$f"
        done
    else
        echo "Error: no manifest file is present; cannot uninstall program!"
        exit 1
    fi

    rm -f manifest

    echo "Successfully uninstalled program."
    exit 0
}

until [ $# -eq 0 ]; do
    case "$1" in
        --uninstall) uninstall;;
        --prefix=*) PREFIX="$(echo $1 | cut -d '=' -f 2)";;
        *) usage;;
    esac
    shift
done

# this function (if finished) would help set up flac2any with default locations
# for required applications by making symlinks to said applications in
# /usr/local/bin/ (the default location)
# FIXME: this doesn't check if the symlinks already exist, and it could be improved
#make_symlinks()
#{
#    cd /usr/local/bin
#    ln -s "$( which flac )"
#    ln -s "$( which metaflac )"
#    ln -s "$( which lame )"
#    ln -s "$( which oggenc )"
#    ln -s "$( which faac )"
#}
#

# for uninstallation
echo "" >manifest

for f in flac2*; do
    install "$f" "${PREFIX}/bin/${f}"
    # log file for uninstallation
    echo "${PREFIX}/bin/${f}" >>manifest
done
